#assembler,linker
NASM := nasm
LINKER := ld

#nasm options 
ELF_FORMAT := -f elf 

#sources and objects 
mm_dir := mm 
sources := $(wildcard *.asm) 
objects := $(patsubst %.asm,%.o,$(sources))

kernel := Kernel
kernel_sources := $(wildcard mm/*.asm *.asm)
kernel_objects := $(patsubst %.asm,%.o,$(kernel_sources))
kernel_script := ../conf/kernel.lds

#ld options
LD_OPTIONS := -T $(kernel_script)

#phony target 
.PHONY:kernel_all $(mm_dir) kernel_clean mm_clean

kernel_all:$(mm_dir) $(objects) $(kernel)

$(mm_dir): 
	$(MAKE) --directory=$@

#assemble *.asm to *.o
SHELL := /bin/bash
$(objects):$(sources)
	for i in $(sources);do \
	$(NASM) $(ELF_FORMAT) $$i;\
	done

#kernel
$(kernel):$(kernel_objects)
	$(LINKER) -o $(kernel) $(LD_OPTIONS) $(kernel_objects)

#clean 
kernel_clean:mm_clean
	-rm $(kernel) $(objects)

mm_clean:
	$(MAKE) --directory=$(mm_dir) mm_clean
